/**
 * 
 */
package tur.br.moradadosol.view.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import tur.br.moradadosol.control.UserAutentication;
import tur.br.moradadosol.control.ValorDiariaControl;
import tur.br.moradadosol.model.ValorDiaria;
import tur.br.moradadosol.view.form.ValorDiariaForm;

/**
 * @author alex
 */
public class ValorDiariaAction extends Action {

	public ActionForward execute(
			ActionMapping mapping,
			ActionForm form,
			HttpServletRequest request,
			HttpServletResponse response)
			throws Exception {

		HttpSession session = request.getSession();

		if(!UserAutentication.isUserAutenticated(session)) {
			return mapping.findForward("login");
		}
		
		ValorDiariaForm dform = (ValorDiariaForm)form;
		ValorDiariaControl vdc = new ValorDiariaControl();
		
		try {
			if(dform!=null) {
				if(dform.getGravar().length()>0) {
					//grava
					vdc.gravar(dform.getDiaria());
					//limpa o form
					dform.reset(mapping, request);
				} else if(dform.getDeletar().length()>0) {
					long cod = dform.getCodigo();
					vdc.deletar(cod);
					
					//limpa o form
					dform.reset(mapping, request);
				} else if(dform.getEditar().length()>0) {
					long cod = dform.getCodigo();
					ValorDiaria vd = vdc.getDiariaByCodigo(cod);
					if(vd!=null) {
						dform.setValorDiaria(vd);
					}
				}
			}
		} catch (Exception e) {
		} finally {
			// carrega a lista
			dform.setDiarias(vdc.getDiariasFuturas());
		}
			
		return mapping.getInputForward();
	}
}
